package com.btpj.blog_server.utils;

import com.btpj.blog_server.entity.Role;
import com.btpj.blog_server.entity.User;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;

public class UserManager {

    /**
     * 获取当前登录的用户
     *
     * @return 当前登录的用户
     */
    public static User getCurrentUser() {
        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 判断当前登录的用户是否是管理员
     *
     * @return 当前登录的用户是否是管理员
     */
    public static boolean isAdmin() {
        List<Role> roles = getCurrentUser().getRoles();
        for (Role role : roles) {
            if (role.getId() == 1) {
                return true;
            }
        }
        return false;
    }
}
